class AdminController < ApplicationController
  before_filter :comprobar_autencidad,:except=>:identificar
  
  def comprobar_autencidad
    unless session[:usuario]
      session[:intended_action] = action_name
      session[:intended_controller] = controller_name
      redirect_to :action => "identificar"
    end
  end
  
  def identificar
    if request.post?  
      session[:usuario] = Usuario.identificar(params[:nombre], params[:clave]).id
      redirect_to :action => session[:intended_action],
                  :controller => session[:intended_controller]
     
    end
  end
  
  def desconectar
    session[:usuario] = nil
    redirect_to inicio
  end
  
  #Acciones administrativas a partir de aquí:
  def index
    list
    render :action => 'lista'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def lista
    @entrada_pages, @entradas = paginate :entradas, :per_page => 10
  end

  def ver
    @entrada = Entrada.find(params[:id])
  end

  def nueva
    @entrada = Entrada.new
  end

  def crear
    @entrada = Entrada.new(params[:entrada])
    if @entrada.save
      flash[:notice] = 'Entrada was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'nueva'
    end
  end

  def editar
    @entrada = Entrada.find(params[:id])
  end

  def modificar
    @entrada = Entrada.find(params[:id])
    if @entrada.update_attributes(params[:entrada])
      flash[:notice] = 'Entrada was successfully updated.'
      redirect_to :action => 'ver', :id => @entrada
    else
      render :action => 'editar'
    end
  end

  def destruir
    Entrada.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

end
